from genuis.authors.models import Author, Invitation 
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from authors.forms import InvitationForm
from django.contrib.auth.models import User
from django.views.decorators.csrf  import csrf_exempt, csrf_protect
from django.core.urlresolvers import reverse

@csrf_exempt
def invite_author(request):
    invitationform = InvitationForm(request.POST or None)
    if request.method == 'GET':
        to_user_id = request.GET['to_user']
    already_created = False
    if invitationform.is_valid():
        to_user_id = request.POST['to_user']
        to_user  = get_object_or_404(User,id=to_user_id)
        message = request.POST['message']
        try:
            invitation = Invitation.objects.get(from_user__pk = request.user.id,
                                                to_user__pk = to_user_id)
            already_created = True
        except Invitation.DoesNotExist:
            invitation = Invitation.objects.create(from_user = request.user,
                                                             to_user = to_user,
                                                             message = message,
                                                             status = False )
        context ={
           'next':'y',
           'alreay_created':already_created,
        }
    else:
        context = {
        'next':'n',
        'to_user_id':to_user_id, 
        'invitationform':invitationform,
        'already_created':already_created,
        } 
        
    return render_to_response(
        'authors/invite.html',
        context,
        context_instance  = RequestContext(request),
    )     
         
@csrf_exempt
def confirm_invitation(request):
    if request.method == 'POST':
         invitation_id = request.POST['invitation_id']
         invitation = Invitation.objects.get(pk=invitation_id)
         invitation.status = False
         invitation.save()
         try:
            author = Author.objects.get(user__pk = request.user.id)
         except Author.DoesNotExist:
            author = Author.objects.create(user = request.user)
         author.myauthors.add(invitation.from_user)
         author.save()
         try:
            co_author = Author.objects.get(user__pk = invitation.from_user.id)
         except Author.DoesNotExist:
            co_author = Author.objects.create(user = invitation.from_user)
         co_author.myauthors.add(invitation.to_user)
         co_author.save()
      
    context = {
        'invitation':invitation,
    }
    return render_to_response(
        'authors/confirmation.html',
        context,
        context_instance =  RequestContext(request),
    )

@csrf_exempt 
def view_invitations(request):
    invitations = Invitation.objects.filter(to_user__pk = request.user.id).exclude(status=True)
    context = {
        'invitations':invitations,
        'invitations_nb':invitations.count(),
    }  
    return render_to_response(
        'authors/invitations.html',
        context,
        context_instance =  RequestContext(request),
    )
